<template>
  <div class="container">
    <div class="form">
      <div class="title">
        <span class="a">LOGIN</span>
        <span class="b">局气后台管理系统</span>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import login from "../api/jurisdiction.js";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "admin123",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login
            .login({
              user: this.ruleForm.username,
              password: this.ruleForm.password,
            })
            .then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  message: "登录成功",
                  type: "success",
                });
                this.$router.push("/layout");
              }else{
                  this.$message.error('登录失败')
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.form {
  width: 580px;
  height: 300px;
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);
  border: 2px solid #427893;
  text-align: center;
  border-radius: 5px;
  line-height: 100px;
}
.a {
  font-size: 35px;
  font-weight: bold;
  color: #32afd9;
}
.b {
  text-indent: 20px;
  font-size: 13px;
  color: gray;
  font-weight: bold;
}
.el-form {
  font-size: 14px;
  color: #729fc0;
  margin: 0 auto;
}
.el-input {
  width: 360px;
  height: 20px;
}
</style>